import GetOrdersModel from '../models/getOrders';
import GetUsersModel from '../models/getUsers';
let Orders = {
    async getOrders (ctx, next) {
        let items = [];
        let counts = 0;
        await GetOrdersModel.get({
            query: ctx.query,
        })
            .then(res => {
                let data = JSON.parse(res.text);
                items = data.data.items;
                counts = data.data.num;

                let ids = '';
                for (let item of items) {
                    if (ids) {
                        ids += ',' + item.member_id;
                    } else {
                        ids += item.member_id;
                    }
                }
                // 查找 关联的用户
                return GetUsersModel.getUserById({
                    query: {
                        ids,
                    },
                });
            })
            .then(res => {
                let data = JSON.parse(res.text);
                let users = data.data;
                for (let item of items) {
                    item.member = users[item.member_id];
                }
                // 公记录当前url的qurery 所以记录之前 query都要置空
                ctx.session.query = {};
                ctx.session.query[ctx.header.referer] = ctx.query;
                ctx.body = {
                    code: 0,
                    page: ctx.query.page,
                    items,
                    counts,
                };
            })
            .catch(err => {
                ctx.body = {
                    code: 500,
                    message: err,
                };
            });
        // let manifest=JSON.parse(fs.readFileSync(path.resolve(__dirname, '../public/rev-manifest.json')));
    },
};
export default Orders;
